<?php

class ContactController extends Zend_Controller_Action
{
	public function init()
	{
		// $this->view->doctype('XHTML1_STRICT');
	}
	
	public function indexAction()
	{
		// create instance form
		$form = new Inc_Form_Contact();
		// show form 
		$this->view->form = $form;
		// if post
		
		if($this->getRequest()->isPost())
		{
			// valid data
			if($form->isValid($this->getRequest()->getPost()))
			{
							
				// for google smtp (mail transport)
				$params = array(
						'ssl' => 'ssl',
						'port' => 465,
						'auth' => 'login',
						'username' => 'dev.rogozhyn@gmail.com',
						'password' => 'dev.account'
				);
				
				$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $params);
				 
				// default smtp server
				Zend_Mail::setDefaultTransport($mailTransport);
				
				// need values array
				$values = $form->getValues();
				
				// $this->view->values = $values
				// print_r($values);
				
				$config = $this->getInvokeArg('bootstrap')->getOption('configs');
				$localConfig = new Zend_Config_Ini($config['localConfigPath']);
				
				$to = (!empty($localConfig->user->salesEmailAddress)) ? 
				$localConfig->user->salesEmailAddress :
				$localConfig->global->defaultEmailAddress;
				
				// create letter
			    $email  = new Zend_Mail();
			
				$email->setBodyText($values['message']);
				$email->setFrom($values['email']);
				$email->addTo('pavlo.rogozhyn@ukrinsoft.com');
				$email->setSubject('test mail');
				$email->send();

				// flash message
				$this->_helper->getHelper('flashMessenger')
				->addMessage('Mail sent');
			
				// redirect success action
			
				$this->_redirect('/contact/success');
			
			}
		}
	}
		
		// this action will be showing message
		
	public function successAction()
	{
		if ($this->_helper->getHelper('flashMessenger')->getMessages())
		{
			$this->view->messages = $this->_helper->getHelper('flashMessenger')
			->getMessages();
		} else {
			$this->_redirect('/');		
	}
		
		
	}
}